﻿using System.Web.Mvc;

using Gx.ColdWarehouseErp.HelperClass;
using System.Collections.Generic;
using Gx.Model.Entity;
using Gx.Bll.Manage;
using Gx.Model.Other;
using Gx.Bll.OtherBll;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using System.Net.Mime;

namespace Gx.ColdWarehouseErp.Controllers
{

    [AboutErrorAttributeMvc]
    [AuthorActionAttributeFilter]
    public class BaseController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult InitDepCombobox(int? id)
        {
            List<E_DepartmentInfo> depList = new E_DepartmentInfoBll().GetDepList(id);
            return Json(depList);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        //public JsonResult InitSocialSecurityCombobox()
        //{
        //    return Json(new M_SocialSecurityInfoBll().GetM_SocialSecurityInfoList());
        //}

        /// <summary>
        /// 获取商品类别json
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public ActionResult GetGoodsTypeJosn(bool isAll)
        //{
        //    List<TreeView> goodsTypeList = new TreeViewBll().GetGoodsTypeJosn(isAll);
        //    return Json(goodsTypeList, JsonRequestBehavior.DenyGet);
        //}


        [HttpPost]
        public ActionResult GetDictJson(int dictType)
        {
            List<M_DictInfo> dictList = new M_DictInfoBll().GetDictInfoList(dictType);
            return Json(dictList, JsonRequestBehavior.DenyGet);
        }


        //[HttpPost]
        //public ActionResult GetBrandJson()
        //{
        //    List<C_BrandInfo> brandList = new C_BrandInfoBll().GetC_BrandInfoList();
        //    return Json(brandList, JsonRequestBehavior.DenyGet);
        //}

        //[HttpPost]
        //public ActionResult GetPriceLevelJosn()
        //{
        //    List<TreeView> priceLevel = new TreeViewBll().GetPriceLevelJosn();
        //    return Json(priceLevel, JsonRequestBehavior.DenyGet);
        //}

        public ActionResult GetDeptJson()
        {
            List<TreeView> DeptList = new TreeViewBll().GetDepJson();
            return Json(DeptList, JsonRequestBehavior.AllowGet);
        }

        //[HttpPost]
        //public ActionResult GetEmployeeBasicJson(string q)
        //{
        //    List<B_EmployeeInfo> empBasicList = new B_EmployeeInfoBll().GetB_EmployeeInfoListLikeFront10(q);
        //    return Json(empBasicList, JsonRequestBehavior.AllowGet);
        //}

        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonNetResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                DateFormatStr = "yyyy-MM-dd HH:mm:ss"
            };
        }

        protected JsonResult Json(bool success, string message)
        {
            return Json(new { Success = success, Msg = message });
        }

        protected JsonResult JsonList<T>(int total, T[] list)
        {
            return Json(new { total, rows = list });
        }

        protected JsonResult JsonList<T, TFooter>(int total, T[] list, TFooter[] footer)
        {
            return Json(new { total, rows = list, footer });
        }
    }

    public class JsonNetResult : JsonResult
    {
        /// <summary>
        /// 日期格式
        /// </summary>
        public string DateFormatStr { get; set; } = "yyyy-MM-dd HH:mm:ss";
        public JsonNetResult()
        {
        }
        public JsonNetResult(object data)
        {

        }
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            if (string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = "application/json";
            }
            else
            {
                response.ContentType = this.ContentType;
            }
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
            if (Data != null)
            {
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting.Indented, DateFormatString = DateFormatStr };
                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
                serializer.Serialize(writer, Data);
                writer.Flush();
            }
        }
    }
}
